package com.xnyy.lottery.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xnyy.lottery.common.CommonResult;
import com.xnyy.lottery.common.page.PageDomain;
import com.xnyy.lottery.common.page.TableSupport;
import com.xnyy.lottery.util.SqlUtil;

import java.util.List;

/**
 * @description:
 * @auther: Administrator
 * @date: 2022/9/16 23:50
 */
public class BaseController {

    /**
     * 设置请求分页数据
     */
    protected void startPage() {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        if (pageNum != null && pageSize != null) {
            String orderBy = SqlUtil.escapeOrderBySql(pageDomain.getOrderBy());
            PageHelper.startPage(pageNum, pageSize, orderBy);
        }
    }

    /**
     * 响应请求分页数据
     */
    @SuppressWarnings({"rawtypes", "unchecked"})
    protected CommonResult getDataTable(List<?> list) {
        CommonResult rspData = CommonResult.success(list);
        rspData.put(CommonResult.TOTAL, new PageInfo(list).getTotal());
        return rspData;
    }

    /**
     * 获取当前登录用户
     *
     * @return
     */
//    protected User currentUser() {
//        final Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
//        final LoginUser loginUser = (LoginUser) authentication.getPrincipal();
//
//        return loginUser.getUser();
//    }
}
